package PyroBots.Vue;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import PyroBots.Controlleur.CSimulateur;
import PyroBots.Modele.Flotte;
import PyroBots.Modele.Robot;
import PyroBots.Vue.Listeners.ButtonFeuListener;
import PyroBots.Vue.Listeners.ButtonPlayListener;
import PyroBots.Vue.Listeners.CarteMouseListener;
import java.util.Observable;
import java.util.Observer;

/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author glen
 * @generated "UML vers Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class VueFlotte extends JPanel implements Observer{ 
	private CSimulateur cSimu = null;
	private ButtonFF jButton_feu = null;
	private JPanel panelRobots = null;
	private JPanel panelListeRobots = null;
	private JButton jButtonAdd = null;
	private JLabel jLabel_flotte = null;
	private VueRobot vueRobot;
	private JLabel jLabel_prop = null;
	private JLabel jLabel_prop_val = null;
	private JPanel jPanel_prop = null;
	private JLabel jLabel_propNom = null;
	private JLabel jLabel_propNom_val = null;
	private JLabel jLabel_propType = null;
	private JLabel jLabel_propType_val = null;
	private JLabel jLabel_propAlgo = null;
	private JLabel jLabel_propAlgo_val = null;
	private JLabel jLabel_propVitesse = null;
	private JLabel jLabel_propVitesse_val = null;
	private JLabel jLabel_propPos = null;
	private JLabel jLabel_propPos_val = null;
	private JLabel jLabel_propX = null;
	private JLabel jLabel_propY = null;
	private JTextField JTF_posX = null;
	private JTextField JTF_posY = null;
	private Boolean premier = true;
		
	public VueFlotte (CSimulateur cSimu){
		super();
		this.cSimu = cSimu;
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		//this.setBackground(new Color(100));
		this.jButtonAdd = new JButton();
		this.jButtonAdd.setText("");
		this.jButtonAdd.setMnemonic(KeyEvent.VK_UNDEFINED);
		this.jButtonAdd.setIcon(new ImageIcon(getClass().getResource("/img/plus.png")));
		this.setBounds(new Rectangle(770, 20, 300, 700));
		
		setjButton_feu(new ButtonFF("/img/feu.png","/img/feu_activated.png"));
		getjButton_feu().setLocation(new Point(0, 0));
		getjButton_feu().setSize(new Dimension(50, 60));
		getjButton_feu().setText("");
				
		this.add(getjButton_feu());
		jButton_feu.addMouseListener(new ButtonFeuListener(cSimu, jButton_feu));
		jLabel_flotte = new JLabel();
		jLabel_flotte.setText("Flotte de robots :");
		//panel robots
		this.panelRobots = new JPanel();
		this.panelListeRobots = new JPanel();
		this.panelRobots.add(jLabel_flotte);
		this.panelRobots.add(jButtonAdd, null);
		JScrollPane jPanel = new JScrollPane(panelListeRobots);
		jPanel.setPreferredSize(new Dimension(300,300));
		panelListeRobots.setPreferredSize(new Dimension(280, 100000));
		this.panelRobots.add(jPanel);
		this.add(panelRobots);
		
		jLabel_propY = new JLabel();
		jLabel_propY.setText("Axe des y :");
		jLabel_propX = new JLabel();
		jLabel_propX.setText("Axe des x :");
		jLabel_propPos = new JLabel();
		jLabel_propPos.setText("Position :");
		jLabel_propPos_val = new JLabel();
		jLabel_propVitesse = new JLabel();
		jLabel_propVitesse.setText("Vitesse :");
		jLabel_propVitesse_val = new JLabel();
		jLabel_propAlgo = new JLabel();
		jLabel_propAlgo.setText("Algorithme :");
		jLabel_propAlgo_val = new JLabel();
		jLabel_propType = new JLabel();
		jLabel_propType_val = new JLabel();
		jLabel_propType.setText("Type :");
		jLabel_propNom = new JLabel();
		jLabel_propNom.setText("Nom :");
		jLabel_propNom_val = new JLabel();
		jLabel_prop = new JLabel();
		jLabel_prop_val = new JLabel();
		jLabel_prop.setText("PROPRIETES DU ROBOT :");
		JTF_posX = new JTextField();
		JTF_posY = new JTextField();
		jPanel_prop = new JPanel();
		jPanel_prop.setPreferredSize(new Dimension(300,200));
		jPanel_prop.setLayout(new GridLayout(8,2));
		jPanel_prop.add(jLabel_prop, null);
		jPanel_prop.add(jLabel_prop_val, null);
		jPanel_prop.add(jLabel_propNom, null);
		jPanel_prop.add(jLabel_propNom_val, null);
		jPanel_prop.add(jLabel_propType, null);
		jPanel_prop.add(jLabel_propType_val, null);
		jPanel_prop.add(jLabel_propAlgo, null);
		jPanel_prop.add(jLabel_propAlgo_val, null);
		jPanel_prop.add(jLabel_propVitesse, null);
		jPanel_prop.add(jLabel_propVitesse_val, null);
		jPanel_prop.add(jLabel_propPos, null);
		jPanel_prop.add(jLabel_propPos_val, null);
		jPanel_prop.add(jLabel_propX, null);
		jPanel_prop.add(jLabel_propY, null);
		jPanel_prop.add(JTF_posX, null);
		jPanel_prop.add(JTF_posY, null);
		this.add(jPanel_prop);
		
		
		jButtonAdd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				DialogAjouterRobot d = new DialogAjouterRobot(VueFlotte.this);
				d.setVisible(true);
				paint_robots();
				repaint();
			}
		});
		
		premier = true;
	}
	
	public void init(){
		cSimu.getcFlotte().getFlotte().addObserver(this);
	}
	

	public CSimulateur getcSimu() {
		return cSimu;
	}


	public void setjButton_feu(ButtonFF jButton_feu) {
		this.jButton_feu = jButton_feu;
	}

	public ButtonFF getjButton_feu() {
		return jButton_feu;
	}
	
	public void paint_robots() {
		this.panelListeRobots.removeAll();
		this.vueRobot = null;
		Flotte flotte = cSimu.getcFlotte().getFlotte();
		for (Robot r : flotte.getRobot()){
			VueRobot vueRobot = new VueRobot(r,this);
			this.panelListeRobots.add(vueRobot);
		}		
	}
	
	
	
	public void paint(Graphics g) {
		super.paint(g);
		// TODO Auto-generated method stub
	
		if(!this.cSimu.isRunning())
			paint_robots();
	
	
		VueRobot selectedRobot = this.getSelectedRobot();
		if(selectedRobot != null){
			Robot r = selectedRobot.getRobot();
			String[] sp = r.getAlgo().getClass().getName().split("\\.");
			this.jLabel_propAlgo_val.setText(sp[sp.length-1]);
			this.jLabel_propNom_val.setText(r.getNom());
			this.jLabel_propType_val.setText(r.getTypeRobot().getNom());
			this.JTF_posX.setText(""+r.getCase().getOrd());
			this.JTF_posY.setText(""+r.getCase().getAbs());
		}
		this.revalidate();
	}

	@Override
	public void update(Observable o, Object arg) {
		this.repaint();
	}
	
	public void setSelectedRobot(VueRobot vueRobot) {
		if(this.vueRobot != null){
			this.vueRobot.setBackground(null);
			this.vueRobot.nomRobot.setForeground(Color.BLACK);
		}
		this.vueRobot = vueRobot;
		if(this.vueRobot != null){
			this.vueRobot.setBackground(Color.DARK_GRAY);
			this.vueRobot.nomRobot.setForeground(Color.BLUE);
		}
	}
	
	public VueRobot getSelectedRobot() {
		if(this.vueRobot != null){
			return this.vueRobot;
		}
		return null;
	}
		
}